/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "event")
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findById", query = "SELECT e FROM Event e WHERE e.id = :id"),
    @NamedQuery(name = "Event.findByName", query = "SELECT e FROM Event e WHERE e.name = :name"),
    @NamedQuery(name = "Event.findByDescription", query = "SELECT e FROM Event e WHERE e.description = :description"),
    @NamedQuery(name = "Event.findByPlace", query = "SELECT e FROM Event e WHERE e.place = :place"),
    @NamedQuery(name = "Event.findByStartDate", query = "SELECT e FROM Event e WHERE e.startDate = :startDate"),
    @NamedQuery(name = "Event.findByEndDate", query = "SELECT e FROM Event e WHERE e.endDate = :endDate"),
    @NamedQuery(name = "Event.findByEndTime", query = "SELECT e FROM Event e WHERE e.endTime = :endTime"),
    @NamedQuery(name = "Event.findByStartTime", query = "SELECT e FROM Event e WHERE e.startTime = :startTime"),
    @NamedQuery(name = "Event.findByDeleted", query = "SELECT e FROM Event e WHERE e.deleted = :deleted"),
    @NamedQuery(name = "Event.findByIsPublic", query = "SELECT e FROM Event e WHERE e.isPublic = :isPublic"),
    @NamedQuery(name = "Event.findByOutdoor", query = "SELECT e FROM Event e WHERE e.outdoor = :outdoor"),
    @NamedQuery(name = "Event.findByParticipant", query = "SELECT e FROM Event e JOIN e.userCollection1 u WHERE u.username = :username"),
    @NamedQuery(name = "event.findPossibleTimeConflict", query = "SELECT e FROM Event e JOIN e.userCollection1 u WHERE u.username = :username AND e.deleted = '0' AND ((e.startDate <= :startDate AND e.endDate >= :startDate) OR (e.startDate >= :startDate AND e.startDate <= :endDate))")})
@XmlRootElement(name = "event")
public class Event implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Basic(optional = false)
    @NotNull(message = "Please insert a name!")
    @Size(min = 1, max = 45)
    private String name;
    
    @Size(max = 140)
    private String description;

    @Size(max = 140)
    private String place;

    @Basic(optional = false)
    @NotNull(message = "Insert a start date!")
    @Temporal(TemporalType.DATE)
    private Date startDate;

    @Basic(optional = false)
    @NotNull(message = "Insert an end date!")
    @Temporal(TemporalType.DATE)
    private Date endDate;

    @Basic(optional = false)
    @NotNull(message = "Insert an end time!")
    @Temporal(TemporalType.TIME)
    private Date endTime;

    @Basic(optional = false)
    @NotNull(message = "Insert a start time!")
    @Temporal(TemporalType.TIME)
    private Date startTime;

    @Basic(optional = false)
    @NotNull
    private boolean deleted;
    
    @Basic(optional = false)
    @NotNull
    private boolean isPublic;
    
    @Basic(optional = false)
    @NotNull
    private boolean outdoor;
    
    @JoinTable(name = "invited", joinColumns = {
        @JoinColumn(name = "idEvent", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "username", referencedColumnName = "username")})
    @ManyToMany
    private Collection<User> userCollection;
    
    @JoinTable(name = "participant", joinColumns = {
        @JoinColumn(name = "idEvent", referencedColumnName = "ID")}, inverseJoinColumns = {
        @JoinColumn(name = "username", referencedColumnName = "USERNAME")})
    @ManyToMany
    private Collection<User> userCollection1;
    
    @ManyToMany(mappedBy = "eventCollection")
    private Collection<Weatherforecast> weatherforecastCollection;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEvent")
    private Collection<Notification> notificationCollection;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEvent")
    private Collection<Weathernotification> weathernotificationCollection;
    
    @JoinColumn(name = "organizer", referencedColumnName = "username")
    @ManyToOne(optional = false)
    private User organizer;

    public Event() {
    }

    public Event(Long id) {
        this.id = id;
    }

    public Event(Long id, String name, Date startDate, Date endDate, Date endTime, Date startTime, boolean deleted, boolean isPublic, boolean outdoor) {
        this.id = id;
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.endTime = endTime;
        this.startTime = startTime;
        this.deleted = deleted;
        this.isPublic = isPublic;
        this.outdoor = outdoor;
    }
    
 
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        if(place != null)
        this.place = place.toLowerCase().trim();
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public boolean getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(boolean isPublic) {
        this.isPublic = isPublic;
    }

    public boolean getOutdoor() {
        return outdoor;
    }

    public void setOutdoor(boolean outdoor) {
        this.outdoor = outdoor;
    }

    /**
     * Getter of the collection of invited people
     * @return the collection of invited people
     */
    @XmlTransient
    public Collection<User> getUserCollection() {
        return userCollection;
    }

    /**
     * Setter of the collection of invited people
     * @param userCollection a collection of invited people
     */
    public void setUserCollection(Collection<User> userCollection) {
        this.userCollection = userCollection;
    }
    
    /**
     * Getter of the collection of participants to the event
     * @return the collection of participants to the event
     */
    @XmlTransient
    public Collection<User> getUserCollection1() {
        return userCollection1;
    }

    /**
     * Setter of the collection of participants to the event
     * @param userCollection a collection of participants to the event
     */
    public void setUserCollection1(Collection<User> userCollection) {
        this.userCollection1 = userCollection;
    }

    @XmlTransient
    public Collection<Weatherforecast> getWeatherforecastCollection1() {
        return weatherforecastCollection;
    }

    public void setWeatherforecastCollection(Collection<Weatherforecast> weatherforecastCollection) {
        this.weatherforecastCollection = weatherforecastCollection;
    }

    @XmlTransient
    public Collection<Notification> getNotificationCollection() {
        return notificationCollection;
    }

    public void setNotificationCollection(Collection<Notification> notificationCollection) {
        this.notificationCollection = notificationCollection;
    }

    @XmlTransient
    public Collection<Weathernotification> getWeathernotificationCollection() {
        return weathernotificationCollection;
    }

    public void setWeathernotificationCollection(Collection<Weathernotification> weathernotificationCollection) {
        this.weathernotificationCollection = weathernotificationCollection;
    }
    
    @XmlTransient
    public User getOrganizer() {
        return organizer;
    }

    public void setOrganizer(User organizer) {
        this.organizer = organizer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "giuliani_mapelli_marocco.entity.Event[ id=" + id + " ]";
    }
    
}
